package cc.pony.wordcralwer.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.swing.text.BadLocationException;
import javax.swing.text.DefaultStyledDocument;
import javax.swing.text.rtf.RTFEditorKit;

public class RtfReader {
	public RtfReader() {
	}


	public static String getTextFromRtf(String filePath) {
		String result = null;
		File file = new File(filePath);
		try {
			DefaultStyledDocument styledDoc = new DefaultStyledDocument();
			InputStream is = new FileInputStream(file);
			new RTFEditorKit().read(is, styledDoc, 0);
			result = new String(styledDoc.getText(0, styledDoc.getLength())
					.getBytes("ISO8859_1"));
			
		} catch (IOException e) {
			e.printStackTrace();
		} catch (BadLocationException e) {
			e.printStackTrace();
		}
		return result;
	}
	
	public static List<String> getWordList(String book){
		Pattern p = Pattern.compile("([a-zA-Z]*)\\s*\\[.*\\]");
		Matcher meanMatcher = p.matcher(book);
		ArrayList<String> list = new ArrayList<String>();
		while(meanMatcher.find()){
			String mean = meanMatcher.group(1);
			list.add(mean);
			System.out.println(mean);
		}
		return list;
	} 
	public static void main(String args[]){
		RtfReader reader = new RtfReader();
		String string= reader.getTextFromRtf("F:\\ieltswords\\word.rtf");
		List<String> list =  getWordList(string);
		System.out.println(list.size());
	}
}